package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syaccp.mapper.InspirationMapper;
import com.syaccp.model.Inspiration;
import com.syaccp.model.SysUser;
import com.syaccp.service.InspirationService;
import com.syaccp.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("InspirationService")
public class InspirationServiceImpl extends ServiceImpl<InspirationMapper, Inspiration> implements InspirationService {

    @Resource
     InspirationMapper inspirationMapper;



    @Resource
    UserService userService;

    public int j=0;

    @Override
    public List<Inspiration> selectAll() {
        List<Inspiration> inspirations = inspirationMapper.selectList(null);
        List<Inspiration> is = new ArrayList<>() ;
        int i;

        for (i = 0; i < inspirations.size(); i++) {
            if (i<inspirations.size()/2){
                is.add(inspirations.get(i));
                j = i;
            }



        }

        return is ;
    }

    @Override
    public List<Inspiration> selectAllTwo() {
        List<Inspiration> inspirations = inspirationMapper.selectList(null);
        List<Inspiration> is = new ArrayList<>();
        for (int i = 1; i < inspirations.size(); i++) {
            if (j<i){
                is.add(inspirations.get(i));
            }

        }
        return is;
    }

    @Override
    public Integer add(Inspiration inspiration) {

        SysUser sysUser = new SysUser();
        sysUser.setUsername(inspiration.getAuthor());
        SysUser s = userService.selectByUserName(sysUser);
        inspiration.setAuthor(s.getName());
        int insert = inspirationMapper.insert(inspiration);



        return insert ;
    }

    @Override
    public Inspiration queryOneById(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Inspiration inspiration = inspirationMapper.selectOne(queryWrapper);
        return inspiration;
    }


}
